﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace Sinacor.Infra.UI.Common
{
    /// <summary>
    /// Classe base para WeakEventManagers
    /// </summary>
    /// <typeparam name="TManager">Type do WeakEventManager</typeparam>
    /// <typeparam name="TSource">Type do Source</typeparam>
    /// <typeparam name="TEventArgs">Type do EventArgs</typeparam>
    public abstract class BaseWeakEventManager<TManager, TSource, TEventArgs> : WeakEventManager
        where TManager : BaseWeakEventManager<TManager, TSource, TEventArgs>, new()
        where TEventArgs : EventArgs
    {
        protected void HandleEvent(object sender, TEventArgs e)
        {
            DeliverEvent(sender, e);
        }

        public static void AddListener(TSource source, IWeakEventListener listener)
        {
            Manager.ProtectedAddListener(source, listener);
        }

        public static void RemoveListener(TSource source, IWeakEventListener listener)
        {
            Manager.ProtectedRemoveListener(source, listener);
        }

        protected static TManager Manager
        {
            get
            {
                Type t = typeof(TManager);
                TManager m = WeakEventManager.GetCurrentManager(t) as TManager;
                if (m == null)
                {
                    m = new TManager();
                    WeakEventManager.SetCurrentManager(t, m);
                }
                return m;
            }
        }
    }
}
